import Mock from 'mockjs'
import '@/mock/extend'

let listDb = Mock.mock({
  'list|500': [
    {
      name: '@cname',
      gender: '@GENDER',
      cardNo: () => {
        return Mock.Random.id()
      },
      email: () => {
        return Mock.Random.first() + '@supcon.com'
      },
      phone: /^1[385][1-9]\d{8}/,
      code: () => {
        return Math.random().toString(36).substr(2)
      },
      status: '@STATUS',
      'id|+1': 1,
      birthday: () => {
        return Mock.Random.date('yyyy-MM-dd')
      },
      remark: ''
    }
  ]
}).list

Mock.mock(/\/api\/user\/list(.*?)/, 'get', options => {
  // 分页信息
  let params = new URLSearchParams(options.url.substr(options.url.indexOf('?')))
  let pageSize = params.get('pageSize') ? params.get('pageSize') : 10
  let currentPage = params.get('currentPage') ? params.get('currentPage') : 1
  let sorter = params.get('sorter')
  let order = params.get('order')
  // 查询参数
  let name = params.get('name')
  let cardNo = params.get('cardNo')
  let birthday = params.get('birthday')
  let phone = params.get('phone')
  let gender = params.get('gender') ? params.get('gender').split(',') : []
  let status = params.get('status') ? params.get('status').split(',') : []

  let results =
    gender.length > 0
      ? listDb.filter(item => gender.indexOf(Number(item.gender).toString()) >= 0)
      : listDb
  results =
    status.length > 0
      ? results.filter(item => status.indexOf(Number(item.status).toString()) >= 0)
      : results

  if (name) {
    results = results.filter(
      item => item.name.indexOf(name) > -1
    )
  }

  if (cardNo) {
    results = results.filter(item => item.cardNo.indexOf(cardNo) > -1)
  }

  if (phone) {
    results = results.filter(item => item.phone.indexOf(phone) > -1)
  }

  if (birthday) {
    results = results.filter(item => item.birthday === birthday)
  }

  if (sorter && order) {
    if (order === 'ascend') {
      results.sort((one, two) => {
        if (one[sorter] > two[sorter]) {
          return 1
        } else if (one[sorter] === two[sorter]) {
          return 0
        } else {
          return -1
        }
      })
    } else {
      results.sort((one, two) => {
        if (one[sorter] > two[sorter]) {
          return -1
        } else if (one[sorter] === two[sorter]) {
          return 0
        } else {
          return 1
        }
      })
    }
  }

  let total = results.length

  if (results.length > pageSize * (currentPage - 1)) {
    results = results.filter(
      (element, index, array) =>
        index >= pageSize * (currentPage - 1) && index < pageSize * currentPage
    )
  } else {
    currentPage = 1
    results = results.filter(
      (element, index, array) => index >= 0 && index < pageSize
    )
  }

  return {
    results: results,
    pagination: {
      total: total,
      pageSize: pageSize,
      currentPage: currentPage
    }
  }
})

Mock.mock(/\/api\/user\/remove(.*?)/, 'post', options => {
  let params = new URLSearchParams(options.url.substr(options.url.indexOf('?')))
  let ids = params.get('ids') ? params.get('ids').split(',') : []
  debugger
  if (ids.length > 0) {
    listDb = listDb.filter(item => ids.indexOf(Number(item.id).toString()) === -1)
  }

  return {
    success: true,
    message: 'success'
  }
})

Mock.mock(/\/api\/user\/submit(.*?)/, 'post', ({ body }) => {
  const item = JSON.parse(body)

  if (item && item.id) {
    let indexItem = listDb.findIndex(i => i.id === item.id)
    if (indexItem > -1) {
      listDb[indexItem] = item
    }
  } else {
    let createKey = 0
    listDb.forEach(function (e) {
      if (e.id >= createKey) {
        createKey = e.id + 1
      }
    })

    item.id = createKey

    if (!item.cardNo || item.cardNo.length !== 18) {
      item.cardNo = Mock.Random.id()
    }

    if (!item.gender || item.cardNo.length !== 18) {
      item.cardNo = Mock.Random.id()
    }

    listDb.unshift(item)
  }

  return {
    success: true,
    message: item
  }
})

Mock.mock(/\/api\/user\/info(.*?)/, 'get', options => {
  let params = new URLSearchParams(options.url.substr(options.url.indexOf('?')))
  let id = params.get('id') ? parseInt(params.get('id')) : undefined
  let result = {
    results: {},
    success: true
  }

  if (!id) {
    result.success = false
    result.message = '请求参数无效!'
  } else {
    result.results = listDb.find((value) => value.id === id)
    if (!result.results) {
      result.success = false
      result.message = '当前单据已经不存在!'
    }
  }
  return result
})
